ShardsManager = { Event = {} };

local soulbags = {};
local globebags = {};
local soulshard = "灵魂碎片";
local driansoul = "吸取灵魂";
local minshards = 1;--最少保留的灵魂碎片数量

local function getbagtypes()
    local i, r;
    r = { };
    r[0] = 0;
    for i = 1, NUM_BAG_SLOTS, 1 do
        local x = GetInventoryItemLink("player", ContainerIDToInventoryID(i));
        if x == nil then
            r[i] = nil;
        else
            r[i] = GetItemFamily(x);
        end;
    end;
    return r;
end;

local function iscanput(bagtype, itemtype)
    return bagtype ~= nil and (bagtype == 0 or bit.band(bagtype, itemtype) ~= 0);
end

local function scanslot(itemLink)
    if not CursorHasItem() then
    
    local i, j, itemtype;
        
        if itemLink == soulshard then
            itemtype = 4;
        else
            itemtype = GetItemFamily(itemLink);
        end;
        
        local bagtypes = getbagtypes();
        
        for i = NUM_BAG_SLOTS, 0, -1 do
            if iscanput(bagtypes[i], itemtype) and GetContainerNumFreeSlots(i) > 0 then return end
        end
        
        local c=0;
        for i = NUM_BAG_SLOTS, 0, -1 do
            local a = bagtypes[i];
            if iscanput(a, itemtype) and (a == 0 or a == 4) then
                for j = GetContainerNumSlots(i),1,-1 do
                    local link = GetContainerItemLink(i, j);
                    if link and GetItemInfo(link) == soulshard then
						c=c+1;
						if(c>minshards)then
	                        PickupContainerItem(i, j);
	                        DeleteCursorItem();
	                        return true;
						end
                    end
                end
            end
        end
    end
    return false;
end

local function usesoulshard(button)
    if not CursorHasItem() and button ~= "LeftButton"
        and GetItemInfo(GetContainerItemLink(this:GetParent():GetID(), this:GetID())) == soulshard then
        PickupContainerItem(this:GetParent():GetID(), this:GetID());
        DeleteCursorItem();
    end;
end

local function looting(link, count)
    local _,_,_,_,_,_,_,stcount = GetItemInfo(link);
    local itemcount = GetItemCount(link);
    if math.ceil((itemcount + count) / stcount) > math.ceil(itemcount / stcount) then
        return scanslot(link);
    end;
    return false;
end

local _LootButton_OnClick = nil;
local function OnLootButtonClick(button)
    if LootSlotIsItem(this.slot) then
        local _,_,c = GetLootSlotInfo(this.slot);
        looting(GetLootSlotLink(this.slot), c);
    end;
    _LootButton_OnClick(button);
end

function ShardsManager.OnLoad()
    local _,cl = UnitClass("player");
    if cl == "WARLOCK" then
        this:RegisterEvent("VARIABLES_LOADED");
        --this:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
        this:RegisterEvent("START_LOOT_ROLL");
        this:RegisterEvent("LOOT_OPENED");
    end
end

function ShardsManager.OnEvent(arg, ...)
    local event = ShardsManager.Event[arg];
    if event then
        event(arg, ...)
    end
end

function ShardsManager.Event.VARIABLES_LOADED(event, ...)
    hooksecurefunc("ContainerFrameItemButton_OnClick", usesoulshard);
    
    if not _LootButton_OnClick then
        _LootButton_OnClick = LootButton_OnClick;
        LootButton_OnClick = OnLootButtonClick;
    end;
end

function ShardsManager.Event.LOOT_OPENED(event, ...)
    local i;
    if arg1 == 1 then
        for i = GetNumLootItems(), 1, -1 do
            if LootSlotIsItem(i) then
                local _,_,c = GetLootSlotInfo(i);
                if (looting(GetLootSlotLink(i), c)) then
                    LootSlot(i);
                end;
            end;
        end
    end;
end

function ShardsManager.Event.START_LOOT_ROLL(event, ...)
    looting(GetLootRollItemLink(arg1), 1);
end
--[[
function ShardsManager.Event.UNIT_SPELLCAST_CHANNEL_START(event, ...)
    if arg1 == "player" and arg2 == driansoul then scanslot(soulshard) end;
end
]]--